package com.example.easypoi.controller;

import com.example.easypoi.mapper.*;
import com.example.easypoi.model.*;
import com.example.easypoi.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * excel 导出控制器
 *
 * @author novel
 * @date 2018/12/27
 */
@RestController
@RequestMapping("city")
public class CityController {

    @Autowired
    private ProvinceMapper provinceMapper;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private TownMapper townMapper;

    @RequestMapping(value = "/import", method = RequestMethod.GET)
    public void importExcel(HttpServletResponse response) throws IOException {
        List<ProvinceVo> provinceVos = new ArrayList<>();

        ProvinceVo provinceVo = provinceMapper.getById(1L);
        List<CityVo> cityVos = cityMapper.getListByProvinceCode(provinceVo.getCode());
        for(CityVo cityVo : cityVos){
            List<TownVo> townVos = townMapper.getListByCityCode(cityVo.getCode());
            cityVo.setTownVos(townVos);
        }
        provinceVo.setCityVos(cityVos);
        provinceVos.add(provinceVo);
        ExcelUtils.exportExcel(provinceVos, null, "地区2", ProvinceVo.class, "地区2", response);
    }

    @RequestMapping(value = "/import2", method = RequestMethod.GET)
    public void importExcel2(HttpServletResponse response) throws IOException {
        List<ProvinceVo> provinceVos = provinceMapper.getAllList();
        for(ProvinceVo provinceVo : provinceVos){
            List<CityVo> cityVos = cityMapper.getListByProvinceCode(provinceVo.getCode());
            provinceVo.setCityVos(cityVos);
        }
        ExcelUtils.exportExcel(provinceVos, null, "地区2", ProvinceVo.class, "地区2", response);
    }
}
